package com.futvan.z.framework.core;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

/**
 * 执行SQL任务
 * @author 42239
 *
 */
public class SQLJob implements StatefulJob {

	public void execute(JobExecutionContext context) throws JobExecutionException {
		//获取SQL
		String sqlinfo =  context.getJobDetail().getJobDataMap().getString("sqlinfo");
		if(z.isNotNull(sqlinfo)){
			//支持多SQL执行
			String [] sqls = sqlinfo.split(";");
			StringBuffer run_info = new StringBuffer();
			try {
				for (int i = 0; i < sqls.length; i++) {
					String sql = sqls[i].trim();
					//获取SQL类型
					String sqltype = sql.substring(0,6).toLowerCase();
					//执行SQL
					if("update".equals(sqltype)){
						int n = z.getSqlSession().update("update",sql);
						run_info.append("执行成功|修改记录数"+ n+"|SQL:"+sql);
					}else if("insert".equals(sqltype)){
						int n = z.getSqlSession().insert("insert",sql);
						run_info.append("执行成功|插入记录数："+ n+"|SQL:"+sql);
					}else if("delete".equals(sqltype)){
						int n = z.getSqlSession().delete("delete",sql);
						run_info.append("执行成功|删除记录数："+ n+"|SQL:"+sql);
					}else{
						run_info.append("执行出错|SQL任务只能执行update、insert、delete语句"+"|SQL:"+sql);
					}
				}
			}catch (Exception e){
				run_info.append("执行SQL任务异常|"+e.getMessage());
			}
			z.LogAndSave("执行SQL任务",run_info.toString(),sqlinfo);
		}else{
			z.LogAndSave("执行SQL任务","执行出错|sqlinfo is null",sqlinfo);
		}
	}

}
